package com.twu.biblioteca;

import java.util.List;

/**
 * Created by dmortime on 07/03/2014.
 */
public class ReturnBookMenu implements IMenu {
    private OutputWriter outputWriter;
    private InputReader inputReader;
    private Customer customer;
    private Library library;

    ReturnBookMenu(OutputWriter outputWriter, InputReader inputReader, Library library, Customer customer) {
        this.outputWriter = outputWriter;
        this.inputReader = inputReader;
        this.library = library;
        this.customer = customer;
    }

    public void display() {
        outputWriter.writeLines("Enter the title of the book you'd like to return:");
    }

    public IMenu nextMenu() {
        String title = inputReader.getInput();
        List<Book> books = customer.fetchBooksByTitle(title);
        if(books.size() > 0) {
            Book theBook = books.get(0);
            if(library.allBooks().contains(theBook)) {
                customer.yieldBook(theBook);
                library.receiveBook(theBook);
                outputWriter.writeLines("Thank you for returning the book.");
            } else {
                outputWriter.writeLines("That is not a valid book to return.");
            }
        } else {
            outputWriter.writeLines("You don't have that book checked out.");
        }
        return new MainMenuWithLoggedInCustomer(outputWriter, inputReader, library, customer);
    }
}
